<<<<<<< HEAD
<?php

namespace app\api\services\store;

use app\api\model\Goods;
use app\api\model\GoodsProduct;
use app\api\model\GoodsSpecification;
use app\api\model\UserCollect;
use app\api\services\BaseServices;
use app\api\services\user\CollectServices;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\Paginator;

class GoodsServices extends BaseServices
{
    /**
     * 分页
     * @param string $is_hot
     * @return Paginator
     * @throws DbException
     */
    public static function list(string $is_hot, $cid): Paginator
    {
        [$page, $list] = self::getPageValue();
        $where = [];
        $where['is_hot'] = $is_hot;
        if (!empty($cid)) {
            $where['cid'] = $cid;
        }
        return Goods::whereNotNull('type')
            ->where($where)
            ->order('sort_order asc')
            ->field('id,goods_image,name,sales,retail_price ')
            ->paginate([
                'list_rows' => $list,
                'page' => $page
            ]);

    }

    /**
     * 商品详情
     * @param int $id
     * @param int $userid
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public static function detail(int $id, int $userid): array
    {
        // 商品信息、商家信息、评论信息
        $goods = Goods::getById($id);
        if (empty($goods)) {
            throw new Exception("商品不存在");
        }
        // 获取规格
        $spec = self::getGoodsSpecification($id);
        // 获取货品
        $product = self::getGoodsProduct($id);
        // 获取评论
        $comment = GoodsCommentServices::getCommentWithUserInfo($id);
        $is_collect = CollectServices::userIsCollect($id, $userid);
        // 是否收藏
        return [
            'spec' => $spec,
            'product' => $product,
            'comment' => $comment,
            'is_collect' => $is_collect
        ];
    }

    /**
     * 获取商品规格
     * @param int $id
     * @return array
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    private static function getGoodsSpecification(int $id): array
    {
        $spec = GoodsSpecification::where('store_goods_id', $id)->select();
        $spec = self::groupArrayByKey($spec, 'specification');
        $finalResult = [];
        foreach ($spec as $k => $v) {
            $finalResult[] = ['name' => $k, 'valuesList' => $v];
        }
        return $finalResult;
    }

    /**
     * 获取规格组合
     * @param int $goodsId
     * @return bool|\PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getGoodsProduct(int $goodsId)
    {
        return GoodsProduct::where('store_goods_id', $goodsId)->select();
    }

    /**
     * @param array $goodsIds
     * @return bool|\PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getGoodsByIds(array $goodsIds)
    {
        return Goods::where('id', 'in', $goodsIds)->select();
    }

    /**
     * 减库存
     * @param $goodsId
     * @param $num
     * @return int
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function addStock($goodsId,$num)
    {
        /** @var GoodsProduct $product */
        $product = GoodsProduct::where('store_goods_id',$goodsId)->find();
        $product->number = $product->number + $num;
        $data = $product->toArray();
        return $product->cas($data);
    }
=======
<?php

namespace app\api\services\store;

use app\api\model\Goods;
use app\api\model\GoodsProduct;
use app\api\model\GoodsSpecification;
use app\api\model\UserCollect;
use app\api\services\BaseServices;
use app\api\services\user\CollectServices;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\Paginator;

class GoodsServices extends BaseServices
{
    /**
     * 商品列表
     * @param string $is_hot
     * @return Paginator
     * @throws DbException
     */
    public static function list(string $is_hot, $cid): Paginator
    {
        [$page, $list] = self::getPageValue();
        $where = [];
//        $where['is_hot'] = $is_hot;
        if (!empty($cid)) {
            $where['category_id'] = $cid;
        }
        return Goods::whereNotNull('type')
            ->where($where)
            ->order('sort_order asc')
            ->field('id,goods_image,name,sales,retail_price ')
            ->paginate([
                'list_rows' => $list,
                'page' => $page
            ]);

    }

    /**
     * 商品详情
     * @param int $id
     * @param int $userid
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public static function detail(int $id, int $userid): array
    {
        // 商品信息、商家信息、评论信息
        $goods = Goods::getById($id);
        if (empty($goods)) {
            throw new Exception("商品不存在");
        }
        $good = Goods::where(['id'=>$id])
        ->order('sort_order asc')
        ->field('id,goods_image,name,sales,retail_price')
        ->find();
        // 获取规格
        $spec = self::getGoodsSpecification($id);
        // 获取货品
        $product = self::getGoodsProduct($id);
        // 获取评论
        $comment = GoodsCommentServices::getCommentWithUserInfo($id);
        $is_collect = CollectServices::userIsCollect($id, $userid);
        // 是否收藏
        return [
            'good'=>$good,
            'spec' => $spec,
            'product' => $product,
            'comment' => $comment,
            'is_collect' => $is_collect
        ];
    }

    /**
     * 获取商品规格
     * @param int $id
     * @return array
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    private static function getGoodsSpecification(int $id): array
    {
        $spec = GoodsSpecification::where('store_goods_id', $id)->field('id,store_goods_id,specification,value,pic_image,num,money')->select();
        $spec = self::groupArrayByKey($spec, 'specification');
        $finalResult = [];
        foreach ($spec as $k => $v) {
            $finalResult[] = ['name' => $k, 'valuesList' => $v];
        }
        return $finalResult;
    }

    /**
     * 获取规格组合
     * @param int $goodsId
     * @return bool|\PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getGoodsProduct(int $goodsId)
    {
        return GoodsProduct::where('store_goods_id', $goodsId)->field('id,specifications,price,number')->select();
    }

    /**
     * @param array $goodsIds
     * @return bool|\PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getGoodsByIds(array $goodsIds)
    {
        return Goods::where('id', 'in', $goodsIds)->select();
    }

>>>>>>> 0591f0f6f4619c1e24959052aec247fc0f331c0b
}